#include <generateData.h>
//打印航点数据
void printWaypointData(uint8_t droneID, int32_t numPoints, const vector<Waypoint>& waypoints) {
    cout << "\n== Waypoint Data ==" << endl;//<<endl除了换行还会强制输出之前在缓冲区的所有数据
    cout << "Drone ID: " << static_cast<int>(droneID) << endl;// droneID为uint8_t,cout 默认会输出 uint8_t 类型的数值为字符//所以强制转换为int类型，输出数值
    cout << "Number of waypoints: " << numPoints << endl;

    for (size_t i = 0; i < waypoints.size(); i++) {
        const auto& wp = waypoints[i];//&表示引用（而不是副本，减少拷贝，提高效率）//const表示引用是只读的，不会修改//auto是自动类型推导//即用wp引用waypoints[i]
        cout << "Waypoint: " << i + 1 << ":" << endl;
        cout << "ID: " << wp.id << endl;
        cout << fixed << setprecision(6) << wp.longitude << endl;
        cout << fixed << setprecision(6) << wp.latitude << endl;
        cout << fixed << setprecision(2) << wp.altitude << endl;
        cout << endl;
    }
     cout << "=============================" << endl;
}

//---------------------------------------//
//打印任务分配结果
void printTaskData(uint32_t droneID, uint32_t targetID, double longitude, double latitude){
    cout << "\n== Task Data ==" << endl;//<<endl除了换行还会强制输出之前在缓冲区的所有数据
    cout << "Drone ID: " << static_cast<int>(droneID) << endl;
    cout << "Target ID: " << static_cast<int>(targetID) << endl;
    cout << "longitude: " << fixed << setprecision(6) << longitude << endl;
    cout << "latitude: " << fixed << setprecision(6) << latitude << endl;
    cout << "=============================" << endl;
}
//---------------------------------------//

//任务分配数据生成
vector<uint8_t> generateTaskData() {
    vector<uint8_t> data(103,0);
    
    //帧头
//    data.push_back(FRAME_HEADER1);
//    data.push_back(FRAME_HEADER2);
    data[0] = FRAME_HEADER1;
    data[1] = FRAME_HEADER2;
    
    //四架飞机状态
    DroneStatus status[4] = {
        DroneStatus::IDLE,
        DroneStatus::BUSY,
        DroneStatus::FAULT,
        DroneStatus::BUSY
    };

    //填充飞机状态和任务数据
    for (int i = 0; i < 4; i++) {
//        int offset = 2 + i * 26;
        int offset = 2 + i * 25;//修改
        data[offset] = static_cast<uint8_t>(status[i]);
        //填充任务目标数据（24字节）
        uint32_t droneID = i + 1;
        uint32_t targetID = 1000 + i;
        double longitude = 116.3 + i * 0.1;
        double latitude = 39.9 + i * 0.1;
//memcpy函数可以将一块内存区域的内容复制到另一块内存区域
// 它需要三个参数：目标内存区域的指针、源内存区域的指针和要复制的字节数。
        memcpy(&data[offset], &droneID, 4);
        memcpy(&data[offset + 4], &targetID, 4);
        memcpy(&data[offset + 8], &longitude, 8);
        memcpy(&data[offset + 16], &latitude, 8);

        //-----------------------------------------//
        // 🔸打印当前 data 位置的地址
//        std::cout << "data ------------ 地址: "
//                  << static_cast<const void*>(&data) << std::endl;
//        std::cout << "data 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data) << std::endl;
//        std::cout << "data[0]地址: "
//                  << static_cast<const void*>(&data[0]) << std::endl;
//        std::cout << "data[0] 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data[0]) << std::endl;
//        std::cout << "data[" << offset << "] 地址: "
//                  << static_cast<const void*>(&data[offset]) << std::endl;
//        std::cout << "data[" << offset << "]地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data[offset]) << std::endl;
//        std::cout << "data[" << offset + 4 << "] 地址: "
//                  << static_cast<const void*>(&data[offset+4]) << std::endl;
//        std::cout << "data[" << offset + 4 << "]地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data[offset+4]) << std::endl;
//        std::cout << "data[" << offset + 8 << "] 地址: "
//                  << static_cast<const void*>(&data[offset+8]) << std::endl;
//        std::cout << "data[" << offset + 8 << "]地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data[offset+8]) << std::endl;
//        std::cout << "data[" << offset + 16 << "] 地址: "
//                  << static_cast<const void*>(&data[offset+16]) << std::endl;
//        std::cout << "data[" << offset + 16 << "]地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&data[offset+16]) << std::endl;
        //-----------------------------------------//

        //----------------------------------//
        printTaskData(droneID, targetID, longitude, latitude);
        //----------------------------------//

    }
    //计算校验和
    uint8_t checksum = 0;
    for (int i = 0; i <102; i++){
        checksum += data[i];
    }
    data[102] = checksum;
    return data;
}

//航点数据生成
vector<uint8_t> generateWaypointData() {
    vector<uint8_t> data;
    vector<Waypoint> waypoints;
    
    //帧头
    data.push_back(FRAME_HEADER1);
    data.push_back(FRAME_HEADER2);

    //飞机号(0-4)
    uint8_t droneID = rand() % 5;
    data.push_back(droneID);

    //航点数量(3-5个)
    int32_t numPoints = 3 + rand() % 3;
    uint8_t numBytes[4];
    memcpy(numBytes, &numPoints, 4);
    data.insert(data.end(), numBytes, numBytes + 4);

    //生成航点数据
    double baseLon = 116.3 + (rand() % 100) * 0.01;
    double baseLat = 39.9 + (rand() % 100) * 0.01;

    for (int i = 0; i < numPoints; i++) {
        Waypoint wp;
        wp.id = 1000 + i;
        wp.longitude = baseLon + (rand() % 500) * 0.01;
        wp.latitude = baseLat + (rand() % 500) * 0.01;
        wp.altitude = rand() % 100 + 30;
        waypoints.push_back(wp);

        uint8_t wpData[28];
        memcpy(wpData, &wp.id, 4);
        memcpy(wpData + 4, &wp.longitude, 8);
        memcpy(wpData + 12, &wp.latitude, 8);
        memcpy(wpData + 20, &wp.altitude, 8);

//        //-----------------------------------------//
//        // 🔸打印当前 wpData 位置的地址
//        std::cout << "wpData ------------ 地址: ";
//        std::cout << "wpData 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&wpData) << std::endl;
//        std::cout << "wpData[0]地址: ";
//        std::cout << "wpData[0] 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&wpData[0]) << std::endl;
//        std::cout << "wpData[4]地址: ";
//        std::cout << "wpData[4] 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&wpData[4]) << std::endl;
//        std::cout << "wpData[12]地址: ";
//        std::cout << "wpData[12] 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&wpData[12]) << std::endl;
//        std::cout << "wpData[20]地址: ";
//        std::cout << "wpData[20] 地址 (十进制): " << reinterpret_cast<std::uintptr_t>(&wpData[20]) << std::endl;
//        //-----------------------------------------//


        data.insert(data.end(), wpData, wpData + 28);

    }

    //--------------------------------//
    // 打印所有航点数据
        printWaypointData(droneID, numPoints, waypoints);
    //---------------------------------//


    //计算校验和
    uint8_t checksum = 0;
    for (auto byte : data) {
        checksum += byte;
    }
    data.push_back(checksum);

    return data;
}

//侦察目标数据生成
vector<uint8_t> generateTargetData() {
    vector<uint8_t> data;
    
    //帧头
    data.push_back(FRAME_HEADER1);
    data.push_back(FRAME_HEADER2);

    //辐射值
    double radiation = 50.0 + (rand() % 500) * 0.1;
    uint8_t radBytes[8];
    memcpy(radBytes, &radiation, 8);
    data.insert(data.end(), radBytes, radBytes + 8);

    //目标坐标
    double longitude = 116.3 + (rand() % 1000) * 0.001;
    double latitude = 39.9 + (rand() % 1000) * 0.001;

    uint8_t lonBytes[8], latBytes[8];
    memcpy(lonBytes, &longitude, 8);
    memcpy(latBytes, &latitude, 8);
    data.insert(data.end(), lonBytes, lonBytes + 8);
    data.insert(data.end(), latBytes, latBytes + 8);
    
    //计算校验和
    uint8_t checksum = 0;
    for (auto byte : data) {
        checksum += byte;
    }

    data.push_back(checksum);
    //打印将要发送的数据
    printTargetData(radiation, longitude, latitude);

    return data;
}

//打印目标数据
void printTargetData(double radiation, double longitude, double latitude) {
    cout << "\n== Target Data ==" << endl;
    cout << fixed << setprecision(2);
    cout << "Radiation: " << radiation << " µSv/h" << endl;
    cout << fixed << setprecision(6);
    cout << "Longitude: " << longitude << endl;
    cout << "Latitude: " << latitude << endl;
    cout << "=============================" << endl;
}


